// pages/Goods/Comment/index.js
const common = require('../../../public.js')
const utils = require('../utils.js')
const config = require('../../../config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFyApp: config.isFyApp,
    storeId: '',
    commnet: [],
    showComment: false,
    commentValue: '',
    inputFocus: false,
    pageNum: 1,
    isloadingBusy: false,
    loadFinished: false,
    active: 3,
    userInfo: {},
    textareaForce: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    this.data.storeId = options.id ? options.id : config.id
    this.data.moduleType = options.moduleType ? options.moduleType : config.type
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.data.token = data.token
        that.data.openid = data.openid
        that.setData({
          userInfo: data.user
        }, function () {
          // 获取商品评论
          that.getCommentList(true)
        })
      }
    }, true)
  },
  textareaBindblur: function () {
    this.setData({
      textareaForce: false
    })
  },
  textareaBindfocus: function () {
    this.setData({
      textareaForce: true
    })
  },
  onChangeTabbar(e) { //切换底部导航
    const index = e.detail.index
    const url = e.detail.url
    if (index != this.data.active) {
      utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
    }
  },
  //获取评论列表
  getCommentList(refresh = false) {
    if (this.data.isloadingBusy === true || this.data.loadFinished === true) {
      return
    }
    this.setData({
      isloadingBusy: true
    })
    const pageNum = refresh ? 1 : this.data.pageNum + 1

    const that = this
    const tempJson = {
      storeId: that.data.storeId,
      pageNum,
      userId: that.data.userInfo.id,
      token: that.data.token
    }
    common.getJson('/store/comments', tempJson, function (res) {
      if (res.status === 200) {
        that.data.pageNum = pageNum
        const finshed = res.data === null || res.data.length === 0 ? true : false
        that.setData({
          commnet: refresh ? res.data : [...that.data.commnet, ...res.data],
          loadFinished: finshed
        })
      } else {
        that.setData({
          loadFinished: true
        })
      }
    }, function () {
      that.setData({
        isloadingBusy: false
      })
    })
  },
  loadMore() {
    this.getCommentList(false)
  },
})